package com.huike.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.dto.TbBusinessGainDTO;
import com.huike.business.domain.dto.TbBusinessPageDTO;
import com.huike.business.domain.vo.TbBusinessVO;
import com.huike.business.service.TbBusinessService;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.SysUser;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.service.ISysUserService;
import com.huike.clues.service.TbAssignRecordService;
import com.huike.common.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
* @author 93238
* @description 针对表【tb_business(商机)】的数据库操作Service实现
* @createDate 2023-10-12 06:08:37
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class TbBusinessServiceImpl extends ServiceImpl<TbBusinessMapper, TbBusiness>
    implements TbBusinessService{

    private final TbBusinessMapper businessMapper;
    private final TbAssignRecordService assignRecordService;

    private final ISysUserService userService;

    @Override
    public Page<TbBusinessVO> getPageList(TbBusinessPageDTO tbBusinessPageDTO) {

        PageHelper.startPage(tbBusinessPageDTO.getPageNum() , tbBusinessPageDTO.getPageSize());

        LocalDate begin = null;
        LocalDate end = null;

        if (tbBusinessPageDTO.getParams().get("beginCreateTime") != "" && tbBusinessPageDTO.getParams().get("endCreateTime") != ""){
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

            String beginCreateTime = (String) tbBusinessPageDTO.getParams().get("beginCreateTime");
            begin = LocalDate.parse(beginCreateTime,formatter);
            String endCreateTime = (String) tbBusinessPageDTO.getParams().get("endCreateTime");
            end = LocalDate.parse(endCreateTime,formatter);
        }

        Page<TbBusinessVO> page = businessMapper.getPage(tbBusinessPageDTO.getId(), tbBusinessPageDTO.getName(),
                tbBusinessPageDTO.getPhone(), tbBusinessPageDTO.getOwner(), tbBusinessPageDTO.getStatus(), begin,end,
                tbBusinessPageDTO.getSubject());
        return page;
    }

    @Override
    public TbBusinessVO mySelectById(Long id) {
        TbBusinessVO tbBusinessVO = businessMapper.mySelectById(id);
        return tbBusinessVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addBusiness(TbBusiness tbBusiness) {
        tbBusiness.setCreateTime(LocalDateTime.now().toString());
        tbBusiness.setCreateBy(SecurityUtils.getUsername());
        businessMapper.insert(tbBusiness);
        log.info("================>"+tbBusiness.getId());
        TbAssignRecord assignRecord = new TbAssignRecord();
        assignRecord.setAssignId(tbBusiness.getId());
        assignRecord.setCreateTime(LocalDateTime.now().toString());
        assignRecord.setCreateBy(SecurityUtils.getUsername());
        assignRecord.setUserId(SecurityUtils.getUserId());
        assignRecord.setUserName(SecurityUtils.getUsername());
        assignRecord.setType("1");
        assignRecord.setDeptId(SecurityUtils.getDeptId());
        assignRecord.setLatest("1");
        assignRecordService.save(assignRecord);
    }

    @Override
    public void assignment(TbBusinessGainDTO tbBusinessGainDTO) {
        List<TbAssignRecord> tbAssignRecords = assignRecordService.listByIds(Arrays.asList(tbBusinessGainDTO.getIds()));
        SysUser user = userService.getById(tbBusinessGainDTO.getUserId());

        tbAssignRecords.forEach(tbAssignRecord -> {
            tbAssignRecord.setUserId(user.getUserId());
            tbAssignRecord.setUserName(user.getUserName());
            tbAssignRecord.setDeptId(user.getDeptId());
            tbAssignRecord.setCreateBy(SecurityUtils.getUsername());
            tbAssignRecord.setCreateTime(LocalDateTime.now().toString());
        });

        assignRecordService.updateBatchById(tbAssignRecords);
    }
}




